package uk.co.accolade.clash.app.intellij;

import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.FileEditorManagerListener;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.FileStatus;
import com.intellij.openapi.vcs.FileStatusManager;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import uk.co.accolade.clash.app.ClashPanel;
import uk.co.accolade.clash.core.ClashFilePath;

import java.io.File;

public class IntelliJFileFacade {

    private FileDocumentManager documentManager;
    private String projectPath;
    private FileStatusManager fileStatusManager;
    private LocalFileSystem localFileSystem;
    private FileEditorManager fileEditorManager;

    public static IntelliJFileFacade getInstance( Project project ) {
        IntelliJFileFacade systemInfo = new IntelliJFileFacade(project.getBaseDir().getPath());
        systemInfo.fileStatusManager = FileStatusManager.getInstance( project );
        systemInfo.documentManager = FileDocumentManager.getInstance();
        systemInfo.localFileSystem = LocalFileSystem.getInstance();
        systemInfo.fileEditorManager = FileEditorManager.getInstance( project );

        return systemInfo;
    }

    protected IntelliJFileFacade( String projectPath ) {
        this.projectPath = projectPath;
    }

    public ClashFilePath getFilename( VirtualFile file ) {
        String filePath = file.getPath();
        return getFilename(filePath);
    }

    public ClashFilePath getFilename( String filePath ) {
        String safeFilePath = filePath.replace("\\", "/");
        return new ClashFilePath( safeFilePath.replace( projectPath, "" ) );
    }

    public VirtualFile fileFromDocument( Document document ) {
        return documentManager.getFile( document );
    }

    public FileStatus getVcsStatus( Document document ) {
        return fileStatusManager.getStatus( fileFromDocument( document ) );
    }

    public VirtualFile virtualFileForPath( ClashFilePath filePath ) {
        File ioFile = new File( projectPath + filePath.getFilePath() );        
        return localFileSystem.findFileByIoFile( ioFile );
    }

    public VirtualFile[] getOpenFiles() {
        return fileEditorManager.getOpenFiles();
    }

    public void removeClashPanel( VirtualFile virtualFile, ClashPanel panel ) {
        FileEditor editor = fileEditorManager.getSelectedEditor( virtualFile );
        if( editor != null ) {
            fileEditorManager.removeTopComponent( editor, panel );
        }
    }

    public void addClashPanel( VirtualFile virtualFile, ClashPanel panel ) {        
        FileEditor editor = fileEditorManager.getSelectedEditor( virtualFile );
        if( editor != null ) {
            fileEditorManager.addTopComponent( editor, panel );
        }
    }

    public void registerAsFileListener( FileEditorManagerListener listener ) {
        fileEditorManager.addFileEditorManagerListener( listener );
    }
}